package com.simple.business.config.utils;

import com.simple.business.service.utilService.SystemConfigService;
import com.simple.business.service.user.UserService;
import com.simple.common.constant.CacheConstants;
import com.simple.common.core.redis.RedisCache;
import com.simple.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @desc
 * @Author Simple
 * @Date 2025/3/13 22:01
 **/
@Slf4j
@Component
public class SimpleUtil {

    @Resource
    private RedisCache redisCache;
    @Resource
    private UserService userService;
    @Resource
    private SystemConfigService systemConfigService;

    /**
     * 校验验证码
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public Boolean validateCaptcha(String username, String code, String uuid) {
        boolean captchaEnabled = systemConfigService.getCaptchaEnabled();;
        if (captchaEnabled) {
            String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
            String captcha = redisCache.getCacheObject(verifyKey);
            if (captcha == null) {
                return Boolean.FALSE;
            }
            redisCache.deleteObject(verifyKey);
            if (!code.equalsIgnoreCase(captcha)) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }
}
